package com.tongzhou.client;

import java.io.UnsupportedEncodingException;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.message.dao.IMessageDAO;
import com.tongzhou.tongzhou.message.dao.impl.MessageDAO;
import com.tongzhou.tongzhou.message.model.Message;
import com.tongzhou.tongzhou.update.model.UpdateCode;
import com.tongzhou.util.Util;

//留言
public class ClientMsg {
	IMessageDAO dao=new MessageDAO();
	public String getInfo(String json){


//		图url、标题、内容、电话号码

		JSONObject jsonObject = JSONObject.fromObject( json );
		String companyNum=null;
		String companyName=null;
		String contactCall=null;
		String content=null;
		try{
			if(jsonObject.get("companyNum")!=null){
				companyNum=(String) jsonObject.get("companyNum");
			}
			if(jsonObject.get("companyName")!=null){
				companyName=(String) jsonObject.get("companyName");
//				try {
//					companyName=new String(companyName.getBytes("ISO-8859-1"),"UTF-8");
//				} catch (UnsupportedEncodingException e) {
//					e.printStackTrace();
//				}
			}
			if(jsonObject.get("contactCall")!=null){
				contactCall=(String) jsonObject.get("contactCall");
			}
			if(jsonObject.get("content")!=null){
				content=(String) jsonObject.get("content");
				try {
					content=new String(content.getBytes("ISO-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer str=new StringBuffer();
		try {
			if(companyName!=null&&contactCall!=null&&content!=null){
				Message message=new Message();
				if(companyNum!=null){
					message.setCompany_num(companyNum);
				}
				message.setCompany_name(companyName);
				message.setContact_call(contactCall);
				message.setContent(content);
				
				dao.addMessage(message);
				return "{'success':0,'msg':'留言成功!'}";
			}else{
				return "{'success':0,'msg':'输入参数有误!'}";
			}
		} catch (DBException e) {
			e.printStackTrace();
		}
		return "{'success':0,'msg':'输入参数有误!'}";
	
//		String str="{'success':1,'msg':'留言成功!'}";
//		return str;
	}
}
